﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Collections;

namespace LdExpress
{
    public class Funds
    {
        public long ID = -1;
        public DateTime CreateDate = DateTime.Now;
        public string FileNumber = "";
        public long IdProvince = 0;
        public long PaymentSubjectId = 0;
        public long SubjectType = 0; //Loai hinh thanh toan : 1-THU KH,2-THU KHÁC,3-TTNCC,4-TTCOM NCC,5-TTCOM KH,6-TT KHÁC
        public string PaymentSubject = "";
        public string Description = "";
        public string BankName = "";
        public double ReciprocalAccount_Had = 0;
        public double ReciprocalAccount_Debt = 0;
        public double CashVND_Thu = 0;
        public double CashVND_Chi = 0;
        public double CashUSD_Thu = 0;
        public double CashUSD_Chi = 0;
        public double BankVND_Thu = 0;
        public double BankVND_Chi = 0;
        public double BankUSD_Thu = 0;
        public double BankUSD_Chi = 0;
        public int CloseFile = 0;
        //Const Payment Method
        public const int Receive_Customer = 1;
        public const int Receive_Other = 2;
        public const int Payment_Agent = 3;
        public const int Payment_Com_Agent = 4;
        public const int Payment_Com_Customer = 5;
        public const int Payment_Other = 6;

        public const string Receive_Customer_Name = "thu kh";
        public const string Receive_Other_Name = "thu khác";
        public const string Payment_Agent_Name = "ttncc";
        public const string Payment_Com_Agent_Name = "ttcom ncc";
        public const string Payment_Com_Customer_Name = "ttcom kh";
        public const string Payment_Other_Name = "tt khác";

        public const int ClosedFile = 1;
        public const int OpenedFile = 0;
        public Funds()
        {
        }

        public Funds(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_Funds";
            if (BankName.Trim().Length == 0) return;
            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "Insert into " + strTableName + " (" +
                                "CreateDate," +
                                "FileNumber," +
                                "IdProvince," +
                                "PaymentSubject," +
                                "PaymentSubjectId," +
                                "SubjectType," +
                                "Description," +
                                "BankName," +
                                "ReciprocalAccount_Had," +
                                "ReciprocalAccount_Debt," +
                                "CashVND_Thu," +
                                "CashVND_Chi," +
                                "CashUSD_Thu," +
                                "CashUSD_Chi," +
                                "BankVND_Thu," +
                                "BankVND_Chi," +
                                "BankUSD_Thu," +
                                "BankUSD_Chi" +
                                ",CloseFile" +
                             ") " +
                         " values(" +
                              DBModule.RefineDatetime(CreateDate) +
                             ",'" + DBModule.RefineString(FileNumber) + "'" +
                             "," + IdProvince.ToString() +
                             "," + "'" + DBModule.RefineString(PaymentSubject) + "'" +
                             "," + PaymentSubjectId.ToString() +
                             "," + SubjectType.ToString() +
                             "," + "'" + DBModule.RefineString(Description) + "'" +
                             "," + "'" + DBModule.RefineString(BankName) + "'" +
                             "," + ReciprocalAccount_Had.ToString() +
                             "," + ReciprocalAccount_Debt.ToString() +
                             "," + CashVND_Thu.ToString() +
                             "," + CashVND_Chi.ToString() +
                             "," + CashUSD_Thu.ToString() +
                             "," + CashUSD_Chi.ToString() +
                             "," + BankVND_Thu.ToString() +
                             "," + BankVND_Chi.ToString() +
                             "," + BankUSD_Thu.ToString() +
                             "," + BankUSD_Chi.ToString() +
                             "," + CloseFile.ToString() +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Funds", "add", cn, trans);
                //GV.Response.Write(strSQL);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                        "CreateDate = " + DBModule.RefineDatetime(CreateDate) + "," +
                        "FileNumber =  '" + DBModule.RefineString(FileNumber) + "'," +
                        "IdProvince =  " + IdProvince.ToString() + "," +
                        "PaymentSubjectId =  " + PaymentSubjectId.ToString() + "," +
                        "SubjectType =  " + SubjectType.ToString() + "," +
                        "PaymentSubject = '" + DBModule.RefineString(PaymentSubject) + "'," +
                        "Description = '" + DBModule.RefineString(Description) + "'," +
                        "BankName = '" + DBModule.RefineString(BankName) + "'," +
                        "ReciprocalAccount_Had = " + ReciprocalAccount_Had + "," +
                        "ReciprocalAccount_Debt = " + ReciprocalAccount_Debt + "," +
                        "CashVND_Thu = " + CashVND_Thu + "," +
                        "CashVND_Chi = " + CashVND_Chi + "," +
                        "CashUSD_Thu = " + CashUSD_Thu + "," +
                        "CashUSD_Chi = " + CashUSD_Chi + "," +
                        "BankVND_Thu = " + BankVND_Thu + "," +
                        "BankVND_Chi = " + BankVND_Chi + "," +
                        "BankUSD_Thu = " + BankUSD_Thu + "," +
                        "BankUSD_Chi = " + BankUSD_Chi +
                        ",CloseFile = " + CloseFile +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("Funds", "edit", cn, trans);
            }
            UpdateReceiveAndPaymentReport(this, cn, trans);
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {

            string strSQL = "DELETE FROM tbl_Funds WHERE ID = " + this.ID.ToString();
            DeleteReceiveAndPaymentReport(this, cn, trans);
            UserObjectsLog.SaveObjectActionLog("Funds", "delete", cn, trans);

            DBModule.ExecuteNonQuery(strSQL, cn, trans);
        }
        public static void DoCloseFile(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "Update tbl_Funds set CloseFile=1";
            DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
        }
        public static void DoOpenFile(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "Update tbl_Funds set CloseFile=0";
            DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
        }
        public static int GetCountStatusFile(int StatusFile, MySqlConnection cn, MySqlTransaction trans)
        {
            int retVal = 0;
            string sqlCommand = "Select Count(ID) From  tbl_Funds where CloseFile=" + StatusFile;
            string Status = DBModule.ExecuteQueryForOneResult(sqlCommand, cn, trans);
            if (Status != null) retVal = int.Parse(Status);
            return retVal;
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_Funds WHERE ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("CreateDate")) CreateDate = DateTime.Parse(dr["CreateDate"].ToString());
                if (!dr.IsNull("FileNumber")) FileNumber = dr["FileNumber"].ToString();
                if (!dr.IsNull("IdProvince")) IdProvince = long.Parse(dr["IdProvince"].ToString());
                if (!dr.IsNull("PaymentSubjectId")) PaymentSubjectId = long.Parse(dr["PaymentSubjectId"].ToString());
                if (!dr.IsNull("SubjectType")) SubjectType = long.Parse(dr["SubjectType"].ToString());
                if (!dr.IsNull("BankName")) BankName = dr["BankName"].ToString();
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("ReciprocalAccount_Had")) ReciprocalAccount_Had = double.Parse(dr["ReciprocalAccount_Had"].ToString());
                if (!dr.IsNull("ReciprocalAccount_Debt")) ReciprocalAccount_Debt = double.Parse(dr["ReciprocalAccount_Debt"].ToString());
                if (!dr.IsNull("CashVND_Thu")) CashVND_Thu = double.Parse(dr["CashVND_Thu"].ToString());
                if (!dr.IsNull("CashVND_Chi")) CashVND_Chi = double.Parse(dr["CashVND_Chi"].ToString());
                if (!dr.IsNull("CashUSD_Thu")) CashUSD_Thu = double.Parse(dr["CashUSD_Thu"].ToString());
                if (!dr.IsNull("CashUSD_Chi")) CashUSD_Chi = double.Parse(dr["CashUSD_Chi"].ToString());
                if (!dr.IsNull("BankVND_Thu")) BankVND_Thu = double.Parse(dr["BankVND_Thu"].ToString());
                if (!dr.IsNull("BankVND_Chi")) BankVND_Chi = double.Parse(dr["BankVND_Chi"].ToString());
                if (!dr.IsNull("BankUSD_Thu")) BankUSD_Thu = double.Parse(dr["BankUSD_Thu"].ToString());
                if (!dr.IsNull("BankUSD_Chi")) BankUSD_Chi = double.Parse(dr["BankUSD_Chi"].ToString());
                if (!dr.IsNull("CloseFile")) CloseFile = int.Parse(dr["CloseFile"].ToString());
            }
        }

        public static void GetList(string Condittion, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select *," +
                " CASE subjectType  WHEN 1 THEN 'THU KH' " +
                " WHEN 2 THEN 'THU KHÁC'" +
                " WHEN 3 THEN 'TTNCC'" +
                " WHEN 4 THEN 'TTCOM NCC'" +
                " WHEN 5 THEN 'TTCOM KH'" +
                " WHEN 6 THEN 'TT KHÁC'" +
                " ELSE ''" +
                " END AS subjectTypeName" +
            " from tbl_Funds where 1=1 ";
            if ((Condittion != "") && (Condittion != null))
            {
                strSQL += Condittion;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            //GV.Response.Write("sql=" + strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_Funds where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
        public void GetBalanceList(string Condittion, string GroupBy, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            ds = null;
            string strSQL = "";
            strSQL = " SELECT SUM(CashVnD_Thu)  AS CashVnD_Thu,SUM(CashVnD_Chi)  AS CashVnD_Chi," +
                    " SUM(CashUSD_Thu)  AS CashUSD_Thu,SUM(CashUSD_Chi)  AS CashUSD_Chi," +
                    " SUM(BankVnD_Thu)  AS BankVnD_Thu,SUM(BankVnD_Chi)  AS BankVnD_Chi," +
                    " SUM(BankUSD_Thu)  AS BankUSD_Thu,SUM(BankUSD_Chi)  AS BankUSD_Chi," +
                    " SUM(CashVnD_Thu-CashVnD_Chi) AS CashVnD_Balance," +
                    " SUM(CashUSD_Thu-CashUSD_Chi) AS CashUSD_Balance," +
                    " SUM(BankVnD_Thu-BankVnD_Chi) AS BankVnD_Balance," +
                    " SUM(BankUSD_Thu-BankUSD_Chi) AS BankUSD_Balance," +
                    " SUM(CashVnD_Thu-CashVnD_Chi + BankVnD_Thu-BankVnD_Chi) AS TotalVND_Balance," +
                    " SUM(CashUSD_Thu-CashUSD_Chi + BankUSD_Thu-BankUSD_Chi) AS TotalUSD_Balance";
            if (GroupBy != null && GroupBy != "")
            {
                if (GroupBy.ToLower() == "bankname")
                    strSQL += ", BankName";
                if (GroupBy.ToLower() == "idprovince")
                    strSQL += " ,tbl_Tinh.Ten";
            }

            strSQL += " FROM tbl_Funds";
            if (GroupBy != null && GroupBy != "")
            {
                if (GroupBy.ToLower() == "idprovince")
                    strSQL += " ,tbl_Tinh ";
            }
            strSQL += " Where 1=1 ";
            if (GroupBy != null && GroupBy != "")
            {
                if (GroupBy.ToLower() == "idprovince")
                    strSQL += " AND tbl_Funds.IdProvince=tbl_Tinh.ID ";
            }
            if (Condittion.Length > 0)
                strSQL += Condittion;
            if ((GroupBy != null) && (GroupBy != ""))
                strSQL = strSQL + " GROUP BY  " + DBModule.RefineString(GroupBy);
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }

        public static void GetListTotalFunds(string strCondition, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "SELECT SUM(Amount_Debt) AS Total_Amount_Debt,SUM(Amount_Paid) AS Total_Amount_Paid, SUM(Amount_Debt-Amount_Paid) AS Total_Amount_Owed" +
            " FROM tbl_borrowbook WHERE 1=1 ";
            if (strCondition.Length > 0)
                strSQL += strCondition;
            strSQL += " GROUP BY IDProvince ";
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public static void UpdateReceiveAndPaymentReport(Funds obj, MySqlConnection cn, MySqlTransaction trans)
        {
            string totalCostID = DBModule.ExecuteQueryForOneResult("SELECT tbl_total_cost_accounting.ID FROM tbl_cost_accounting,tbl_total_cost_accounting" +
                    " where tbl_cost_accounting.ID=tbl_total_cost_accounting.CostAccountingID AND tbl_cost_accounting.FileNumber='" + obj.FileNumber + "'", cn, trans);
            if (totalCostID != null)
            {
                string sqlCommand = "";
                DataSet ds = null;
                //loại hình TT là: Thu khách hàng ; TKDU Có là 131
                //Sổ quỹ                                  link sang               Báo cáo phải thu
                //Ngày tháng                      =>                             Cột “ Ngày TT”
                //Bank                                 =>                             Cột “ HTTT”
                //Số tiền ( cột thu)            =>                               Cột “Số tiền đã thu” 
                if (obj.SubjectType == Funds.Receive_Customer && obj.ReciprocalAccount_Debt == 131)
                {
                    sqlCommand = "SELECT  SUM(CashVND_Thu+BankVND_Thu) AS TotalAmountReceive" +
                        " FROM tbl_funds WHERE FileNumber='" + obj.FileNumber + "' AND ReciprocalAccount_Debt=131 AND SubjectType=" + Funds.Receive_Customer +
                        " GROUP BY FileNumber";
                    ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
                    if (ds != null)
                        sqlCommand = "Update tbl_total_cost_accounting set Receivables_Update=" + DBModule.RefineDatetime(obj.CreateDate) +
                            ",Receivables_PaymentMethod='" + obj.BankName + "', Receivables_Obtained = " + ds.Tables[0].Rows[0]["TotalAmountReceive"].ToString() +
                            ",Receivables_Must_Obtained=TotalSellInvoice -" + ds.Tables[0].Rows[0]["TotalAmountReceive"].ToString() +
                            " where ID =" + totalCostID;
                }
                //loại hình TT là: TT COM KH ; TKDU Nợ là 338
                //Sổ quỹ                                  link sang               Báo cáo phải thu
                //Số tiền ( Cột chi)                     =>                       Cột “ Đã thanh toán COM”
                else if (obj.SubjectType == Funds.Payment_Com_Customer && obj.ReciprocalAccount_Had == 338)
                {
                    sqlCommand = "SELECT  SUM(CashVND_Chi+BankVND_Chi) AS TotalAmountPayment" +
                      " FROM tbl_funds WHERE FileNumber='" + obj.FileNumber + "' AND ReciprocalAccount_Had=338 AND SubjectType=" + Funds.Payment_Com_Customer +
                      " GROUP BY FileNumber";
                    ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
                    if (ds != null)
                        sqlCommand = "Update tbl_total_cost_accounting set Receivables_Paid_Com= " + ds.Tables[0].Rows[0]["TotalAmountPayment"].ToString() +
                            ",Receivables_Must_Paid_Com=TotalComCustomer - TotalTaxCustomer - " + ds.Tables[0].Rows[0]["TotalAmountPayment"].ToString() +
                            " where ID =" + totalCostID;
                }

                    //loại hình TT là: TT COM NCC ; TKDU Nợ là 338
                //Sổ quỹ                                  link sang               Báo cáo phải trả
                //Số tiền ( Cột chi)                     =>                       Cột “ Đã thanh toán COM”
                else if (obj.SubjectType == Funds.Payment_Com_Agent && obj.ReciprocalAccount_Had == 338)
                {
                    sqlCommand = "SELECT  SUM(CashVND_Chi+BankVND_Chi) AS TotalAmountPayment" +
                     " FROM tbl_funds WHERE FileNumber='" + obj.FileNumber + "' AND ReciprocalAccount_Had=338 AND SubjectType=" + Funds.Payment_Com_Agent +
                     " GROUP BY FileNumber";
                    ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
                    if (ds != null)
                        sqlCommand = "Update tbl_total_cost_accounting set Payables_Paid_Com = " + ds.Tables[0].Rows[0]["TotalAmountPayment"].ToString() +
                            ",Payables_Must_Paid_Com=TotalComAgent - TotalTaxAgent- " + ds.Tables[0].Rows[0]["TotalAmountPayment"].ToString() +
                            " where ID =" + totalCostID;
                }

                //loại hình TT là: TT NCC ; TKDU Nợ là 331
                //Sổ quỹ                                  link sang               Báo cáo phải trả
                //Ngày tháng                      =>                             Cột “ Ngày TT”
                //Bank                                 =>                             Cột “ HTTT”
                //Số tiền ( cột chi)            =>                               Cột “Số tiền đã trả” 
                else if (obj.SubjectType == Funds.Payment_Agent && obj.ReciprocalAccount_Had == 331)
                {
                    sqlCommand = "SELECT  SUM(CashVND_Chi+BankVND_Chi) AS TotalAmountPayment" +
                      " FROM tbl_funds WHERE FileNumber='" + obj.FileNumber + "' AND ReciprocalAccount_Had=331 AND SubjectType=" + Funds.Payment_Agent +
                      " GROUP BY FileNumber";
                    ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
                    if (ds != null)
                        sqlCommand = "Update tbl_total_cost_accounting set Payables_Update=" + DBModule.RefineDatetime(obj.CreateDate) +
                           ",Payables_PaymentMethod='" + obj.BankName + "', Payables_Payment = " + ds.Tables[0].Rows[0]["TotalAmountPayment"].ToString() +
                           ",Payables_Must_Payment=TotalBuyInvoice -" + ds.Tables[0].Rows[0]["TotalAmountPayment"].ToString() +
                           " where ID =" + totalCostID;
                }
                if (sqlCommand.Length > 0)
                {
                    //GV.Response.Write(sqlCommand);
                    DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                }
            }
        }
        public static void DeleteReceiveAndPaymentReport(Funds obj, MySqlConnection cn, MySqlTransaction trans)
        {
            string totalCostID = DBModule.ExecuteQueryForOneResult("SELECT tbl_total_cost_accounting.ID FROM tbl_cost_accounting,tbl_total_cost_accounting" +
                    " where tbl_cost_accounting.ID=tbl_total_cost_accounting.CostAccountingID AND tbl_cost_accounting.FileNumber='" + obj.FileNumber + "'", cn, trans);
            if (totalCostID != null)
            {
                string sqlCommand = "";
                double TotalAmountReceive = obj.CashVND_Thu + obj.BankVND_Thu;
                double TotalAmountPayment = obj.CashVND_Chi + obj.BankVND_Chi;
                DataSet ds = null;
                //loại hình TT là: Thu khách hàng ; TKDU Có là 131
                //Sổ quỹ                                  link sang               Báo cáo phải thu
                //Ngày tháng                      =>                             Cột “ Ngày TT”
                //Bank                                 =>                             Cột “ HTTT”
                //Số tiền ( cột thu)            =>                               Cột “Số tiền đã thu” 
                if (obj.SubjectType == Funds.Receive_Customer && obj.ReciprocalAccount_Debt == 131)
                {
                    sqlCommand = "Update tbl_total_cost_accounting set Receivables_Update=" + DBModule.RefineDatetime(obj.CreateDate) +
                        ",Receivables_PaymentMethod='" + obj.BankName + "', Receivables_Obtained=Receivables_Obtained-" + TotalAmountReceive +
                        ",Receivables_Must_Obtained=TotalSellInvoice +" + TotalAmountReceive +
                        " where ID =" + totalCostID;
                }
                //loại hình TT là: TT COM KH ; TKDU Nợ là 338
                //Sổ quỹ                                  link sang               Báo cáo phải thu
                //Số tiền ( Cột chi)                     =>                       Cột “ Đã thanh toán COM”
                else if (obj.SubjectType == Funds.Payment_Com_Customer && obj.ReciprocalAccount_Had == 338)
                {
                    sqlCommand = "Update tbl_total_cost_accounting set Receivables_Paid_Com=Receivables_Paid_Com - " + TotalAmountPayment +
                        ",Receivables_Must_Paid_Com=TotalComCustomer - TotalTaxCustomer + " + TotalAmountPayment +
                        " where ID =" + totalCostID;
                }

                //loại hình TT là: TT COM NCC ; TKDU Nợ là 338
                //Sổ quỹ                                  link sang               Báo cáo phải trả
                //Số tiền ( Cột chi)                     =>                       Cột “ Đã thanh toán COM”
                else if (obj.SubjectType == Funds.Payment_Com_Agent && obj.ReciprocalAccount_Had == 338)
                {
                    sqlCommand = "Update tbl_total_cost_accounting set Payables_Paid_Com=Payables_Paid_Com - " + TotalAmountPayment +
                        ",Payables_Must_Paid_Com=TotalComAgent - TotalTaxAgent + " + TotalAmountPayment +
                        " where ID =" + totalCostID;
                }

            //loại hình TT là: TT NCC ; TKDU Nợ là 331
                //Sổ quỹ                                  link sang               Báo cáo phải trả
                //Ngày tháng                      =>                             Cột “ Ngày TT”
                //Bank                                 =>                             Cột “ HTTT”
                //Số tiền ( cột chi)            =>                               Cột “Số tiền đã trả” 
                else if (obj.SubjectType == Funds.Payment_Agent && obj.ReciprocalAccount_Had == 331)
                {

                    sqlCommand = "Update tbl_total_cost_accounting set Payables_Update=" + DBModule.RefineDatetime(obj.CreateDate) +
                       ",Payables_PaymentMethod='" + obj.BankName + "', Payables_Payment=Payables_Payment- " + TotalAmountPayment +
                       ",Payables_Must_Payment=Payables_Must_Payment + " + TotalAmountPayment +
                       " where ID =" + totalCostID;
                }
                if (sqlCommand.Length > 0)
                {
                    //GV.Response.Write(sqlCommand);
                    DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
                }
            }
        }
    }
}

